define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'user/user/index',
                    add_url: 'user/user/add',
                    edit_url: 'user/user/edit',
                    // del_url: 'user/user/del',
                    multi_url: 'user/user/multi',
                    index_href_url: 'user/index',
                    table: 'user',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'user.id',
                searchFormVisible: true,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), sortable: true},
                        // {field: 'group.id', title: __('Group'), searchList: $.getJSON("ajax/get_groups"), formatter: Controller.api.formatter.group},
                        {field: 'username', title: __('Username'), operate: 'LIKE'},
                        {field: 'nickname', title: __('Nickname'), operate: 'LIKE'},
                        {field: 'mobile', title: __('Mobile'), operate: 'LIKE'},
                        {field: 'code', title: __('邀请码')},

                        {field: 'consume_res', title: __('Consume_res')+'%',operate: false},
                        {field: 'contribute_res', title: __('Contribute_res')+'%',operate: false},
                        {field: 'integer_res', title: __('积分返点')+'%',operate: false},

                        {field: 'money', title: __('Money'),operate: false, sortable: true},
                        {field: 'integral', title: '积分',operate: false},
                        {field: 'score', title: __('Score'),operate: false, sortable: true},
                        {field: 'parent_id', title: __('Parent agent')+'id', formatter: Controller.api.formatter.parent_agent},
                        {field: 'server_type', title: __('Server type'),
                            searchList: {"0": __('前端用户'), "1": __('省服务商'),"2": __('市服务商'),"3": __('区服务商')
                                ,"5": __('大区服务商')} , formatter: Controller.api.formatter.server_type},
                        {field: 'is_agent', title: __('Is agent'), formatter: Table.api.formatter.status, searchList: {1: __('是'), 0: __('否')}},
                        {field: 'is_shop', title: __('Is shop'), formatter: Table.api.formatter.status, searchList: {1: __('是'), 0: __('否')}},
                        {field: 'province', title: __('Province'), operate:false},
                        {field: 'city', title: __('City'), operate:false},
                        {field: 'area', title: __('Area'), operate:false},
                        {field: 'regione', title: __('大区'), operate:false},

                        {field: 'logintime', title: __('Logintime'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                        {field: 'loginip', title: __('Loginip'), formatter: Table.api.formatter.search, operate: false},

                        {field: 'status', title: __('Status'), formatter: Table.api.formatter.status, searchList: {normal: __('Normal'), hidden: __('Hidden')}},
                        {field: 'status', title: __('启用禁止'), operate: false, formatter: Controller.api.formatter.custom},

                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();


            $('.server_type').change(function (){

                var this_id = $(this).val();
                if(this_id == 0){
                    $('.is_agent_div').show();
                    $('.is_shop_div').show();
                }else{
                    $('.is_agent_div').hide();
                    $('.is_shop_div').hide();
                }
            })


        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            formatter:{
                parent_agent:function (value, row, index) {
                    if(value <= 0) return '-';
                    return '<a href="'+'?parent_id='+value+'">id:' + value + ' <br> ' +row.parent_agent_name+'</a>';
                },
                server_type:function (value, row, index) {
                    if(value == 0) return '前端用户';
                    if(value == 1) return '省服务商';
                    if(value == 2) return '市服务商';
                    if(value == 3) return '区服务商';
                    if(value == 5) return '大区服务商';
                },

                group:function (value, row, index) {
                    return row.group.name;
                },

                custom: function (value, row, index) {
                    //添加上btn-change可以自定义请求的URL进行数据处理
                    return '<a class="btn-change text-success" data-url="user/user/statusAajax" data-id="' + row.id + '"><i class="fa ' + (row.status == 'hidden' ? 'fa-toggle-off' : 'fa-toggle-on') + ' fa-2x"></i></a>';
                },


            }
        }
    };
    return Controller;
});